package com.yanqu.road.server.manager.tradewar;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.rank.UnionActivityRank;
import com.yanqu.road.entity.rank.cross.CrossUserRank;
import com.yanqu.road.logic.bussiness.rank.RankBussiness;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.cross.CrossActivityRankListModel;
import com.yanqu.road.logic.cross.CrossActivityUnionRankListModel;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.manager.activity.ActivityMgr;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossTradeWarRankMgr extends TempMgr {

    private static Map<Integer, CrossActivityRankListModel> tradeWarRankListModelMap;

    private static Map<Integer, CrossActivityUnionRankListModel> tradeWarUnionRankListModelMap;

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    @Override
    public boolean save() {
        for(CrossActivityRankListModel rankListModel : tradeWarRankListModelMap.values()){
            if(rankListModel.isUpdate()){
                rankListModel.saveCrossTradeWarRankList();
            }
        }
        for(CrossActivityUnionRankListModel rankListModel : tradeWarUnionRankListModelMap.values()){
            if(rankListModel.isUpdate()){
                rankListModel.saveCrossTradeWarUnionRankList();
            }
        }
        return true;
    }

    @Override
    public boolean reloadData() {
        CrossTradeWarActivityMgr.loadActivity();
        tradeWarRankListModelMap = new ConcurrentHashMap<>();
        initCrossRankListModelMap();
        tradeWarUnionRankListModelMap = new ConcurrentHashMap<>();
        initCrossUnionRankListModelMap();
        return true;
    }

    private void initCrossRankListModelMap(){
        synchronized (tradeWarRankListModelMap) {
            Map<Integer, ActivityInfo> activityInfoMap = CrossTradeWarActivityMgr.getOpenActivityMap();
            List<Integer> activityIdList = new ArrayList<>();
            for (ActivityInfo activityInfo : activityInfoMap.values()) {
                activityIdList.add(activityInfo.getActivityId());
            }
            Map<Integer, LinkedList<CrossUserRank>> crossUserRankListMap = RankBussiness.getCrossTradeWarRankListMap(activityIdList);
            for (Map.Entry<Integer, LinkedList<CrossUserRank>> dataEntry : crossUserRankListMap.entrySet()) {
                CrossActivityRankListModel rankListModel = new CrossActivityRankListModel(dataEntry.getKey(), true);
                ActivityInfo activityInfo = activityInfoMap.get(dataEntry.getKey());
                int conditionType = eGamePlayerEventType.CrossTradeWarScoreRiseRank.getValue();
                int maxRank = ActivityHelper.getMaxRankByConditionType(conditionType, CrossTradeWarActivityMgr.getActivityConditionInfoList(activityInfo.getActivityId()));
                if (maxRank <= 0) {
                    maxRank = GameConfig.RANK_DEFAULT_MAX_COUNT;
                }
                rankListModel.setRankNum(maxRank);
                rankListModel.setRankList(dataEntry.getValue());
                tradeWarRankListModelMap.put(dataEntry.getKey(), rankListModel);
            }
        }
    }

    private void initCrossUnionRankListModelMap(){
        synchronized (tradeWarUnionRankListModelMap) {
            Map<Integer, ActivityInfo> activityInfoMap = CrossTradeWarActivityMgr.getOpenActivityMap();
            List<Integer> activityIdList = new ArrayList<>();
            for (ActivityInfo activityInfo : activityInfoMap.values()) {
                activityIdList.add(activityInfo.getActivityId());
            }
            Map<Integer, LinkedList<UnionActivityRank>> crossUnionRankListMap = RankBussiness.getCrossTradeWarUnionRankListMap(activityIdList);
            for (Map.Entry<Integer, LinkedList<UnionActivityRank>> dataEntry : crossUnionRankListMap.entrySet()) {
                CrossActivityUnionRankListModel rankListModel = new CrossActivityUnionRankListModel(dataEntry.getKey());
                ActivityInfo activityInfo = activityInfoMap.get(dataEntry.getKey());
                int conditionType = eGamePlayerEventType.CrossTradeWarUnionScoreRiseRank.getValue();
                int maxRank = ActivityHelper.getMaxRankByConditionType(conditionType, CrossTradeWarActivityMgr.getActivityConditionInfoList(activityInfo.getActivityId()));
                if (maxRank <= 0) {
                    maxRank = GameConfig.RANK_DEFAULT_MAX_COUNT;
                }
                rankListModel.setRankNum(maxRank);
                rankListModel.setRankList(dataEntry.getValue());
                tradeWarUnionRankListModelMap.put(dataEntry.getKey(), rankListModel);
            }
        }
    }

    public static CrossActivityRankListModel getCrossActivityRankListModel(int activityId){
        return tradeWarRankListModelMap.get(activityId);
    }

    public static CrossActivityUnionRankListModel getCrossActivityUnionRankListModel(int activityId){
        return tradeWarUnionRankListModelMap.get(activityId);
    }

    public static void changeTradeWarRank(int activityId, CrossUserRank crossUserRank){
        CrossActivityRankListModel rankListModel = getCrossActivityRankListModel(activityId);
        if(null == rankListModel){
            synchronized (tradeWarRankListModelMap){
                if(!tradeWarRankListModelMap.containsKey(activityId)){
                    ActivityInfo activityInfo = CrossTradeWarActivityMgr.getActivityInfoIfNotLoadDb(activityId);
                    if(null != activityInfo){
                        CrossActivityRankListModel crossActivityRankListModel = new CrossActivityRankListModel(activityInfo.getActivityId(), true);
                        int conditionType = eGamePlayerEventType.CrossTradeWarScoreRiseRank.getValue();
                        int maxRank = ActivityHelper.getMaxRankByConditionType(conditionType, CrossTradeWarActivityMgr.getActivityConditionInfoList(activityInfo.getActivityId()));
                        if(maxRank <= 0){
                            maxRank = GameConfig.RANK_DEFAULT_MAX_COUNT;
                        }
                        crossActivityRankListModel.setRankNum(maxRank);
                        tradeWarRankListModelMap.put(activityId, crossActivityRankListModel);
                    }
                }
            }
            rankListModel = getCrossActivityRankListModel(activityId);
        }
        if(null != rankListModel){
            rankListModel.rankChange(crossUserRank);
        }
    }

    public static void changeTradeWarUnionRank(int activityId, UnionActivityRank unionRank){
        CrossActivityUnionRankListModel rankListModel = getCrossActivityUnionRankListModel(activityId);
        if(null == rankListModel){
            synchronized (tradeWarUnionRankListModelMap){
                if(!tradeWarUnionRankListModelMap.containsKey(activityId)){
                    ActivityInfo activityInfo = CrossTradeWarActivityMgr.getActivityInfoIfNotLoadDb(activityId);
                    if(null != activityInfo){
                        CrossActivityUnionRankListModel crossActivityUnionRankListModel = new CrossActivityUnionRankListModel(activityInfo.getActivityId());
                        int conditionType = eGamePlayerEventType.CrossTradeWarUnionScoreRiseRank.getValue();
                        int maxRank = ActivityHelper.getMaxRankByConditionType(conditionType, CrossTradeWarActivityMgr.getActivityConditionInfoList(activityInfo.getActivityId()));
                        if(maxRank <= 0){
                            maxRank = GameConfig.RANK_DEFAULT_MAX_COUNT;
                        }
                        crossActivityUnionRankListModel.setRankNum(maxRank);
                        tradeWarUnionRankListModelMap.put(activityId, crossActivityUnionRankListModel);
                    }
                }
            }
            rankListModel = getCrossActivityUnionRankListModel(activityId);
        }
        if(null != rankListModel){
            rankListModel.rankChange(unionRank);
        }
    }

    /**
     * 获取玩家名次
     */
    public static int getCrossActivityRank(int activityId, long serverId, long userId) {
        int rank = -1;
        CrossActivityRankListModel rankListModel = getCrossActivityRankListModel(activityId);
        if(null != rankListModel){
            rank = rankListModel.getMyRank(userId, serverId);
        }
        int maxRank = getMaxRank(activityId, eGamePlayerEventType.CrossTradeWarScoreRiseRank.getValue());
        if(rank > maxRank){
            rank = -1;
        }
        return rank;
    }

    /**
     * 获取商会名次
     */
    public static int getCrossActivityUnionRank(int activityId, long serverId, String unionUid) {
        int rank = -1;
        CrossActivityUnionRankListModel rankListModel = getCrossActivityUnionRankListModel(activityId);
        if(null != rankListModel){
            rank = rankListModel.getMyRank(serverId, unionUid);
        }
        int maxRank = getMaxRank(activityId, eGamePlayerEventType.CrossTradeWarUnionScoreRiseRank.getValue());
        if(rank > maxRank){
            rank = -1;
        }
        return rank;
    }

    /**
     * 获取活动配置的最大名次
     */
    private static int getMaxRank(int activityId, int conditionType){
        int maxRank = GameConfig.RANK_DEFAULT_MAX_COUNT;
        ActivityInfo activityInfo = CrossTradeWarActivityMgr.getActivityInfo(activityId);
        if(null != activityInfo) {
            maxRank = ActivityHelper.getMaxRankByConditionType(conditionType, CrossTradeWarActivityMgr.getActivityConditionInfoList(activityInfo.getActivityId()));
        }
        return maxRank;
    }
}